/**
 * BBD Service Inc
 * All Rights Reserved @2018
 */
package com.xiaoke.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.xiaoke.utils.JqgridPage;
import com.xiaoke.utils.QueryFilters;
import com.xiaoke.utils.data.PageModel;
import com.xiaoke.web.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 輪播管理
 *
 * @author
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @RequestMapping(value = "/list")
    public String list(Model model) {
        return "order/list";
    }

    @RequestMapping("/query")
    //  @ResponseBody
    public void query(int page, int rows, String sidx, String sord, String filters, HttpServletResponse response) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setPageNo(page);
        filters_.setPageSize(rows);
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        PageModel pageModel = orderService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        JqgridPage page1 = new JqgridPage((long) pageModel.getTotalRecord(), pageModel.getTotalPage(), pageModel.getPageNo(),
                pageModel.getResults());
        writeInfo(response, JSON.toJSONString(page1));
    }

    /**
     * 向客户端写数据
     *
     * @param response
     * @param info
     */
    public void writeInfo(HttpServletResponse response, String info) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.getWriter().write(info);
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
